// import app from '@/utils/app'
export const PORT = ''
export const RequestMethod = {
  POST: 'post',
  GET: 'get',
  DELETE: "delete",
  PUT: "put"
}
/**
 * 封装Axios
 * 处理请求、响应错误信息
 */
// 引入一个提示信息的消息组件
import axios from 'axios';


// const baseURL = process.env.VUE_APP_SERVER_URL || '';
// const baseURL = import.meta.env.VITE_APP_BASE_API;
// console.log('baseURL master-3',baseURL)

// create an axios instance
const service = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API, // url = base url + request url
    // baseURL: 'https://test-api.tearingspaces.com/api/app', // 所有异步请求都加上/api,nginx转发到后端Springboot
    withCredentials: true, // send cookies when cross-domain requests
    timeout: 30000 // request timeout
})

// request interceptor
service.interceptors.request.use(
  config => {
    const token =  localStorage.getItem("token") || ''
    if (token && config.headers) {
      config.headers['AUTH-TOKEN'] = token
    }

    return config
  },
  error => {
    // do something with request error
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    // debugger
    const res = response.data;
      // console.log('response',response?.config?.url,response.data.msg)
    if (res.code === 200 || res.code === 5001) {
      return Promise.resolve(res);
    } else {
        if(res.code === 2007){
            // 禁用用户提示
            // const forbiddenShow =app._instance.exposed.forbiddenShow;
            // forbiddenShow(res?.data)
        }
        return Promise.reject({
            code: res.code,
            data: [],
            msg: res.msg
        })
    }
  },
  error => {
      console.error('error',error)
  }
)

export default service //导出封装后的axios

